package io.xxx.omni.mdc.site.service

import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import io.xxx.omni.mdc.model.*
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.cache.annotation.CacheConfig
import org.springframework.cache.annotation.Cacheable
import org.springframework.stereotype.Service

@Service
@CacheConfig(cacheNames = ["stores"])
class StoreService {

    private val log = LoggerFactory.getLogger(javaClass)

    @Autowired
    private lateinit var platformMapper: PlatformMapper

    @Autowired
    private lateinit var storeMapper: StoreMapper

    @Autowired
    private lateinit var authorizationMapper: AuthorizationMapper

    @Cacheable
    fun getAll(pid: String?, enabled: Boolean?): List<Store> {
        log.info("Store getAll")
        val wrapper = KtQueryWrapper(Store::class.java)
        if (pid != null)
            wrapper.eq(Store::pid, pid)
        if (enabled != null)
            wrapper.eq(Store::enabled, enabled)
        val stores = storeMapper.selectList(wrapper)
        if (stores.isNullOrEmpty())
            return emptyList()

        val platformWrapper = KtQueryWrapper(Platform::class.java)
            .`in`(Platform::id, stores.map { it.pid })
        val platforms = platformMapper.selectList(platformWrapper)
            .associateBy { it.id }
        for (store in stores)
            store.platform = platforms[store.pid]

        var authorizationWrapper = KtQueryWrapper(Authorization::class.java)
            .`in`(Authorization::sid, stores.map { it.id })
        val authorizations = authorizationMapper.selectList(authorizationWrapper)
            .associateBy { it.sid }
        for (store in stores) {
            var authorization = authorizations[store.id]
            if (authorization == null) {
                authorizationWrapper = KtQueryWrapper(Authorization::class.java)
                    .eq(Authorization::pid, store.pid)
                authorization = authorizationMapper.selectOne(authorizationWrapper)
            }
            store.authorization = authorization
        }

        return stores
    }

    @Cacheable
    fun getOne(id: String): Store? {
        val wrapper = KtQueryWrapper(Store::class.java)
            .eq(Store::id, id)
        val store = storeMapper.selectOne(wrapper) ?: return null

        val platform = platformMapper.selectById(store.pid)
        store.platform = platform

        var authorizationWrapper = KtQueryWrapper(Authorization::class.java)
            .eq(Authorization::sid, store.id)
        var authorization = authorizationMapper.selectOne(authorizationWrapper)
        if (authorization == null) {
            authorizationWrapper = KtQueryWrapper(Authorization::class.java)
                .eq(Authorization::pid, store.pid)
            authorization = authorizationMapper.selectOne(authorizationWrapper)
        }
        store.authorization = authorization
        return store
    }
}